/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newconsulta.java
 *
 * Created on 30/Dez/2011, 15:45:30
 */

package vetclini;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import org.jdesktop.application.Action;
import veterinario.Animal;
import veterinario.Consulta;
import veterinario.Medico;

/**
 *
 * @author 2011101993 - Hugo Amaro
 * @author 2006130415 - Jorge Martins Alves
 */
public class newconsulta extends javax.swing.JPanel {

    private VetCliniView pai;
    private JFrame minhaframe;

    /** Creates new form newconsulta */
    public newconsulta(JFrame fra) {
        initComponents();
        this.minhaframe = fra;
    }

    public void setPai(VetCliniView parent){
        this.pai = parent;
    }

    public void setMedicos(){
        medicos.removeAll();
        String[] listaNova = new String[pai.getClinica().medicos().size()];
        for(int i=0; i<pai.getClinica().medicos().size(); i++){
            listaNova[i] = pai.getClinica().medicos().get(i).getNome();
        }
        medicos.setListData(listaNova);
    }

    public void setClientes(){
        cliente.removeAllItems();
        String[] listaNova = new String[pai.getClinica().clientes().size()];
        for(int i=0; i<pai.getClinica().clientes().size(); i++){
            listaNova[i] = pai.getClinica().clientes().get(i).getNome();
            cliente.addItem(listaNova[i]);
        }
        setAnimais();
    }

    public void setAnimais(){
        animal.removeAllItems();
        ArrayList<Animal> clienteAnimais = pai.getClinica().clientes().get(cliente.getSelectedIndex()).getAnimais();
        String[] listaNova = new String[clienteAnimais.size()];
        for(int i=0; i<clienteAnimais.size(); i++){
            listaNova[i] = clienteAnimais.get(i).getId()+" "+clienteAnimais.get(i).getRaca();
            animal.addItem(listaNova[i]);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        ano = new javax.swing.JTextField();
        mes = new javax.swing.JTextField();
        dia = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        tipo = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descricao = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        animal = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cliente = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        medicos = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        concluir = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vetclini.VetCliniApp.class).getContext().getResourceMap(newconsulta.class);
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        ano.setText(resourceMap.getString("ano.text")); // NOI18N
        ano.setName("ano"); // NOI18N

        mes.setText(resourceMap.getString("mes.text")); // NOI18N
        mes.setName("mes"); // NOI18N

        dia.setText(resourceMap.getString("dia.text")); // NOI18N
        dia.setName("dia"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Clínica", "Domicílio" }));
        tipo.setName("tipo"); // NOI18N
        tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoActionPerformed(evt);
            }
        });

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        descricao.setColumns(20);
        descricao.setRows(5);
        descricao.setName("descricao"); // NOI18N
        jScrollPane1.setViewportView(descricao);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        animal.setName("animal"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        cliente.setName("cliente"); // NOI18N
        cliente.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                clientePropertyChange(evt);
            }
        });

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        medicos.setName("medicos"); // NOI18N
        jScrollPane2.setViewportView(medicos);

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(vetclini.VetCliniApp.class).getContext().getActionMap(newconsulta.class, this);
        concluir.setAction(actionMap.get("clickedConcluir")); // NOI18N
        concluir.setText(resourceMap.getString("butaoConcluir.text")); // NOI18N
        concluir.setName("butaoConcluir"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(26, 26, 26)
                                .addComponent(concluir))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(ano, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(animal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jLabel7))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(animal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(concluir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(35, 35, 35))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_tipoActionPerformed

    private void clientePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_clientePropertyChange
        // TODO add your handling code here:
        setAnimais();
    }

    @Action
    public void clickedConcluir() {
        boolean podeConcluir = true;
        String erro = "";
        int a=0, m=0, d=0;
        try{
            d = Integer.parseInt(dia.getText());
            m = Integer.parseInt(mes.getText());
            a = Integer.parseInt(ano.getText());
        }
        catch (java.lang.NumberFormatException ne) {
            erro += "/ data inválida\r\n";
            podeConcluir = false;
            a=0; m=0; a=0;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        if(d>0&&m>0&&m<=12&&a>0){

            Date testDate = null;
            try
	    {
	      testDate = dateFormat.parse(dia.getText()+"/"+m+"/"+ano.getText());
	    }
            catch (ParseException e)
	    {
	      erro += "/ data inválida (1)\r\n";

              podeConcluir = false;
	    }

            String data = "";
            if(d<10)
                data += 0;
            data += d+"/";
            if(m<10)
                data += 0;
            data += m+"/";
            data += a;
            if (!dateFormat.format(testDate).equals(data))
	    {
	      erro += "/ "+dateFormat.format(testDate).toString()+"data inválida (2)\r\n";
	      podeConcluir = false;
	    }

        }else {
            erro += "/ data inválida (3)\r\n";
	    podeConcluir = false;
        }
        GregorianCalendar dataI = new GregorianCalendar(a,m-1,d);

        if(medicos.getSelectedIndices().length==0){
            erro += "/ sem Médicos seleccionados ";
	    podeConcluir = false;
        }

        if(podeConcluir){
            ArrayList<Medico> osMedicos = new ArrayList<Medico>();
            for(int i=0; i<medicos.getSelectedIndices().length; i++)
                osMedicos.add(pai.getClinica().medicos().get(medicos.getSelectedIndices()[i]));
            Consulta novaConsulta = new Consulta(dataI, ""+descricao.getText(), pai.getClinica().clientes().get(cliente.getSelectedIndex()).getAnimais().get(animal.getSelectedIndex()), osMedicos);
            novaConsulta.setTipo(tipo.getSelectedIndex());

            pai.getClinica().addConsulta(novaConsulta);
            pai.updateLists();
            this.minhaframe.dispose();
        }else{
            janelaErro janelaErro = new janelaErro(erro);
            janelaErro.pack();
            janelaErro.setVisible(true);
            janelaErro.requestFocus();
        }
    }//GEN-LAST:event_clientePropertyChange


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox animal;
    private javax.swing.JTextField ano;
    private javax.swing.JComboBox cliente;
    private javax.swing.JButton concluir;
    private javax.swing.JTextArea descricao;
    private javax.swing.JTextField dia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList medicos;
    private javax.swing.JTextField mes;
    private javax.swing.JComboBox tipo;
    // End of variables declaration//GEN-END:variables

}
